home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / XSTRCMP.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  2KB  |  68 lines

  1. /*
  2. **  xstrcmp() - compares strings using DOS wildcards
  3. **              'mask' may contain '*' and '?'
  4. **               returns 1 if 's' matches 'mask', otherwise 0
  5. **               public domain by Steffen Offermann 1991
  6. */
  7.  
  8.  
  9. int xstrcmp (char *mask, char *s)
  10. {
  11.       while (*mask)
  12.       {
  13.             switch (*mask)
  14.             {
  15.             case '?':
  16.                   if (!*s)
  17.                         return (0);
  18.                   s++;
  19.                   mask++;
  20.                   break;
  21.  
  22.             case '*':
  23.                   while (*mask == '*')
  24.                         mask++;
  25.                   if (!*mask)
  26.                         return ( 1 );
  27.                   if (*mask == '?')
  28.                         break;
  29.                   while (*s != *mask)
  30.                   {
  31.                         if (!*s)
  32.                               return (0);
  33.                         s++;
  34.                   }
  35.                   s++;
  36.                   mask++;
  37.                   break;
  38.  
  39.             default:
  40.                   if (*s != *mask)
  41.                         return (0);
  42.                   s++;
  43.                   mask++;
  44.             }
  45.       }
  46.  
  47.       if (!*s && *mask)
  48.             return (0);
  49.       return ( 1 );
  50. }
  51.  
  52. #ifdef TEST
  53.  
  54. #include <stdio.h>
  55.  
  56. void main(int argc, char *argv[])
  57. {
  58.       if (3 != argc)
  59.       {
  60.             puts("Usage: XSTRCMP string_1 string_2");
  61.             return;
  62.       }
  63.       printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
  64.             xstrcmp(argv[1], argv[2]));
  65. }
  66.  
  67. #endif /* TEST */
  68.